﻿using Microsoft.EntityFrameworkCore;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.ApproveManager;
using SJ.Platform.EFCore.Model.ProjectManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Project
{
    public class Project_Requirement_FeatureHandle
    {
        private readonly SJDBContext _context;

        public Project_Requirement_FeatureHandle(SJDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(ProjectRequirementFeature entity)
        {
            try
            {
                _context.ProjectRequirementFeatures.Add(entity);
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>Create", ex);
                return false;
            }
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> BatchCreateAsync(List<ProjectRequirementFeature> list)
        {
            try
            {
                int result = 0;
                if (list != null && list.Count > 0)
                {
                    foreach (ProjectRequirementFeature entity in list)
                    {
                        _context.ProjectRequirementFeatures.Add(entity);
                    }
                    result = await _context.SaveChangesAsync();
                }

                return result == list.Count;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>BatchCreateAsync", ex);
                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(ProjectRequirementFeature entity)
        {
            try
            {
                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>Update", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(ProjectRequirementFeature entity)
        {
            try
            {
                _context.ProjectRequirementFeatures.Remove(entity);
                return _context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>Delete", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(string id)
        {
            try
            {
                var entity = _context.ProjectRequirementFeatures.Find(id);
                if (entity != null)
                {
                    _context.ProjectRequirementFeatures.Remove(entity);
                    await _context.SaveChangesAsync();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>DeleteById", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据父级id删除
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByPIdAsync(string pId)
        {
            try
            {
                var infos = await _context.ProjectRequirementFeatures.Where(info => info.PId == pId).ToListAsync();
                int count = _context.Database.ExecuteSqlRaw($"delete from project_requirement_feature where Pid='{pId}';");
                return infos.Count == count;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>DeleteByPIdAsync", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据项目id删除
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByProjectIdAsync(string projectId)
        {
            try
            {
                var infos = await _context.ProjectRequirementFeatures.AsNoTracking().Where(info => info.ProjectId == projectId).ToListAsync();
                int count = _context.Database.ExecuteSqlRaw($"delete from project_requirement_feature where ProjectId='{projectId}';");
                return infos.Count == count;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>DeleteByProjectIdAsync", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProjectRequirementFeature> GetEntityByIdAsync(string id)
        {
            try
            {
                return await _context.ProjectRequirementFeatures.FindAsync(id);
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Project_Requirement_FeatureHandle=>GetEntityById", ex);
            }
            return null;
        }
    }
}
